// pages/search/index.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    searchText:'',//搜索
    currentPage:0,
    list:[],
    query:{
      page:1
    },
    currentUser:null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    const that = this
    that.getList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  onChange(e) {
    this.data.searchText = e.detail
  },
  doSearch() {
    const that = this
    that.data.currentPage = 0;
    that.data.query.search = that.data.searchText
    that.getList();
  },
  getList() {
    console.log('get page')
    const that = this
    if(that.data.currentPage == 0) {
      that.setData({
        list:[]
      })
    }
    console.log(that.data.list)
    that.data.query.page = that.data.currentPage + 1;
    app.api.get(app.getUrl('volunteer.list'), that.data.query).then((res)=>{
      if(that.data.currentPage < res.pagination.pages) {
        that.data.list = that.data.list.concat(res.data)
        console.log(that.data.list)
        that.setData({
          list: that.data.list,
          currentPage:res.pagination.current
        })
        if(!that.data.currentUser && res.data.length>0) {
          that.setData({
            currentUser:res.data[0]
          })
        }

      }
    })
  },
  onClickLeft() {
    wx.navigateBack({
      delta: 0,
    })
  },
  selectVolunteer(e) {
    let key = e.currentTarget.dataset.index;
    let user = this.data.list[key];
    if(user) {
      this.setData({
        currentUser:user
      })
    }
  },
  onReachBottom() {
    const that = this
    that.getList();
  }
})